﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DataSet_fp_DyhTableAdapters;

public partial class search : System.Web.UI.Page
{
    //public int seqID;
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
            int seqID;
            string keyword = Request.QueryString["keyword"];
            string type = Request.QueryString["sel"];
            if (type != "")
            {
                HiddenField_type.Value = type;
            }
            //else
            //{
            //    HiddenField_type.Value = "1";
            //}
            //Label_MPnumber.Text = Request.QueryString["keyword"].ToString().Trim();
            //Label_MPnumber_1.Text = Request.QueryString["keyword"].ToString().Trim();
            //Label_MPnumber_2.Text = Request.QueryString["keyword"].ToString().Trim();
            //Label_MPnumber_3.Text = Request.QueryString["keyword"].ToString().Trim();


            //Dyh_baseDBTableAdapter baseta = new DataSet_fp_DyhTableAdapters.Dyh_baseDBTableAdapter(); 
            //DataSet_fp_Dyh.Dyh_baseDBDataTable baseDataTable = baseta.Get_Data_By_keyword_and_type(keyword.ToString().Trim(), Convert.ToByte(type));
            
            //Dyh_searchTableAdapter searchta=new Dyh_searchTableAdapter();
            ////DataSet_fp_Dyh.Dyh_searchDataTable searchDataTable=searchta.
            //if ((int)baseDataTable.Rows.Count > 0)
            //{
            //    baseta.Update_S_times_Query(keyword.ToString().Trim(), Convert.ToByte(type));
            //    baseta.Update_L_S_time_Query(keyword.ToString().Trim(), Convert.ToByte(type));
            //    seqID = (int)baseDataTable.Rows[0]["id"];

            //    HiddenField_seqID.Value = seqID.ToString().Trim();

            //    //Response.Write(seqID);
            //    searchta.Insert_New_Data_Query(seqID,Public.GetClientIP() ,Public.GetClientMAC(),Page.Server.MachineName.ToString(),Page.Request.UserHostName,"","",0,3,0);  //写入当前查询记录
            //}  //Request.ServerVariables["REMOTE_ADDR"].ToString() .GetClientMAC().GetMACAddressString()
            //else
            //{
            //    baseta.Insert_New_Data_Query(keyword.ToString().Trim(), Convert.ToByte(type), 1, 0);
            //    baseDataTable = baseta.Get_Data_By_keyword_and_type(keyword.ToString().Trim(), Convert.ToByte(type));

            //    seqID = (int)baseDataTable.Rows[0]["id"];
            //    HiddenField_seqID.Value = seqID.ToString().Trim();
            //    //Response.Write(seqID);
            //    //baseta.ins
            //    searchta.Insert_New_Data_Query(seqID, Public.GetClientIP(), Public.GetClientMAC(), Page.Server.MachineName.ToString(), Page.Request.UserHostName, "", "", 0, 3, 0);  //写入当前查询记录
            //}
        //}
        
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadioButtonList radioButtonList_report = (RadioButtonList)FormView1.FindControl("RadioButtonList1");
        TextBox textBox_report_others = (TextBox)FormView1.FindControl("TextBox_Others");
        if (radioButtonList_report.SelectedValue.Trim() == "其他")
        {
            textBox_report_others.Visible = true;
        }
        else
        {
            textBox_report_others.Visible = false;
        }
    }
    protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
         //判断该内容是否已经举报或查询过
        TextBox textBox_keyword = (TextBox)FormView1.FindControl("TextBox_content");
        DropDownList dropDownlist_type = (DropDownList)FormView1.FindControl("DropDownList_type");

        string keyword = textBox_keyword.Text.Trim();
        string type = dropDownlist_type.SelectedValue.Trim();
        int seqID;




        Dyh_baseDBTableAdapter baseta = new DataSet_fp_DyhTableAdapters.Dyh_baseDBTableAdapter();
        DataSet_fp_Dyh.Dyh_baseDBDataTable baseDataTable = baseta.Get_Data_By_keyword_and_type(keyword, Convert.ToByte(type));

        //Dyh_searchTableAdapter searchta = new Dyh_searchTableAdapter();
        //DataSet_fp_Dyh.Dyh_searchDataTable searchDataTable=searchta.
        if ((int)baseDataTable.Rows.Count > 0)
        {


            baseta.Update_R_times_Query(keyword, Convert.ToByte(type));
            baseta.Update_L_R_time_Query(keyword, Convert.ToByte(type));
            seqID = (int)baseDataTable.Rows[0]["id"];

            //此处需加入判断本IP地址是否已经对本信息进行过举报，若举报过，则跳转到已经举报过的提示页面
            Dyh_reportTableAdapter reportta = new Dyh_reportTableAdapter();
            if ((int)reportta.Get_Data_By_seqID_and_IP(Public.GetClientIP(), seqID).Rows.Count > 0)
            {
                string url ="doreport.aspx?"+"sel=" + type + "&keyword=" + keyword + "&done=1";
                Response.Redirect(url);
            }

            HiddenField_seqID.Value = seqID.ToString().Trim();

            //searchta.Insert_New_Data_Query(seqID, Public.GetClientIP(), Public.GetClientMAC(), Page.Server.MachineName.ToString(), Page.Request.UserHostName, "", "", 0, 3, 0);  //写入当前查询记录
        }  //Request.ServerVariables["REMOTE_ADDR"].ToString() .GetClientMAC().GetMACAddressString()
        else
        {
            baseta.Insert_New_Data_Query(keyword, Convert.ToByte(type), 0, 0);
            baseDataTable = baseta.Get_Data_By_keyword_and_type(keyword, Convert.ToByte(type));
            //IpSearch.GetAddressWithIP(Public.GetClientIP()).Trim())
            seqID = (int)baseDataTable.Rows[0]["id"];
            HiddenField_seqID.Value = seqID.ToString().Trim();
            //Response.Write(seqID);
            //baseta.ins
            //searchta.Insert_New_Data_Query(seqID, Public.GetClientIP(), Public.GetClientMAC(), Page.Server.MachineName.ToString(), Page.Request.UserHostName, "", "", 0, 3, 0);  //写入当前查询记录
        }






        //对举报的内容“其它”及“不作选择，只发表评论”进行特殊处理
        RadioButtonList radioButtonList_report = (RadioButtonList)FormView1.FindControl("RadioButtonList1");
        TextBox textBox_report_others = (TextBox)FormView1.FindControl("TextBox_Others");

        if (radioButtonList_report.SelectedValue.Trim() == "其他")
        {
            e.Values["R_type"] = textBox_report_others.Text.Trim();
        }
        else if (radioButtonList_report.SelectedValue.Trim() == "不作选择，只发表评论")
        {
            e.Values["R_type"] = " ";
        }
        else
        {
            e.Values["R_type"] = radioButtonList_report.SelectedValue.Trim();
        }

        e.Values["seqID"] = Convert.ToInt32(HiddenField_seqID.Value);
        e.Values["IP"] = Public.GetClientIP();
        e.Values["MAC"] = Public.GetClientMAC();
        e.Values["R_typeID"] =Convert.ToByte(Request.QueryString["sel"]);

        //优化举报物理地址及用户名
        TextBox textBox_IP_from = (TextBox)FormView1.FindControl("IP_fromTextBox");
        if (textBox_IP_from.Text.Trim() == IpSearch.GetAddressWithIP(Public.GetClientIP()).Trim())
        {
            e.Values["IP_from"] = textBox_IP_from.Text.Trim() + "用户";
            //}
            //else
            //{
            //        e.Values["IP_from"]="";
        }


        //判断是否为第一次举报，决定如何更新第一次举报时间及最后一次举报时间
        //Dyh_baseDBTableAdapter baseta = new DataSet_fp_DyhTableAdapters.Dyh_baseDBTableAdapter();

        if ((int)baseta.Get_Data_By_keyword_and_type(keyword, Convert.ToByte(type)).Rows[0]["R_times"] > 0)
        {
            baseta.Update_L_R_time_Query(keyword, Convert.ToByte(type));
            baseta.Update_R_times_Query(keyword, Convert.ToByte(type));
        }
        else
        {
            baseta.Update_F_R_time_Query(keyword, Convert.ToByte(type));
            baseta.Update_L_R_time_Query(keyword, Convert.ToByte(type));
            baseta.Update_R_times_Query(keyword, Convert.ToByte(type));
        }

        //DataSet_fp_Dyh.Dyh_baseDBDataTable baseDataTable = baseta.Get_Data_By_keyword_and_type(keyword.ToString().Trim(), Convert.ToByte(type));


    }
    protected void RadioButtonList1_DataBound(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
            RadioButtonList radioButtonList_report = (RadioButtonList)FormView1.FindControl("RadioButtonList1");
            radioButtonList_report.Items.FindByValue("不作选择，只发表评论").Selected = true;
        //}
    }
    protected void FormView1_DataBound(object sender, EventArgs e)
    {
        string keyword = Request.QueryString["keyword"];
        string type = Request.QueryString["sel"];

        //写入转入IP地址归属地
        TextBox textBox_IP_from = (TextBox)FormView1.FindControl("IP_fromTextBox");
        textBox_IP_from.Text = IpSearch.GetAddressWithIP(Public.GetClientIP());

        //如果页面跳转时，举报关健字串非空，在举报字串样写入此值
        if ((keyword != null) && (type != null))
        {
            TextBox textBox_keyword = (TextBox)FormView1.FindControl("TextBox_content");
            DropDownList dropDownlist_type = (DropDownList)FormView1.FindControl("DropDownList_type");
            textBox_keyword.Text = keyword.Trim();
            textBox_keyword.ReadOnly = true;
            textBox_keyword.Enabled = false;
            dropDownlist_type.SelectedValue = type.Trim();
            dropDownlist_type.Enabled = false;
        }
    }
    protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        //此处要加入成功添加报告记录后的举报次数的更新，及页面跳转。
        TextBox textBox_keyword = (TextBox)FormView1.FindControl("TextBox_content");
        DropDownList dropDownlist_type = (DropDownList)FormView1.FindControl("DropDownList_type");

        string url = "doreport.aspx?sel=" + dropDownlist_type.SelectedValue.Trim() + "&keyword=" + textBox_keyword.Text+"&done=0";
        Response.Redirect(url);
    }
    protected void DropDownList_type_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList dropDownlist_type = (DropDownList)FormView1.FindControl("DropDownList_type");
        HiddenField_type.Value = dropDownlist_type.SelectedValue.ToString();


        //针对不同的类别，对输入框进行验证；
        if (dropDownlist_type.SelectedValue == "1")
        {
            RegularExpressionValidator Validator1 = (RegularExpressionValidator)FormView1.FindControl("Validator1");
            //RequiredFieldValidator Validator1 = (RequiredFieldValidator)FormView1.FindControl("Validator0");
            Validator1.Enabled = true;
            Validator1.Visible = true;
        }
        else if (dropDownlist_type.SelectedValue == "2")
        {
            RegularExpressionValidator Validator2 = (RegularExpressionValidator)FormView1.FindControl("Validator2");
            Validator2.Enabled = true;
            Validator2.Visible = true;
        }
        else if (dropDownlist_type.SelectedValue == "3")
        {
            RegularExpressionValidator Validator3 = (RegularExpressionValidator)FormView1.FindControl("Validator3");
            Validator3.Enabled = true;
            Validator3.Visible = true;
        }
        else if (dropDownlist_type.SelectedValue == "4")
        {
            RegularExpressionValidator Validator4 = (RegularExpressionValidator)FormView1.FindControl("Validator4");
            Validator4.Enabled = true;
            Validator4.Visible = true;
        }
        else if (dropDownlist_type.SelectedValue == "5")
        {
            RegularExpressionValidator Validator5 = (RegularExpressionValidator)FormView1.FindControl("Validator5");
            Validator5.Enabled = true;
            Validator5.Visible = true;
        }
        else if (dropDownlist_type.SelectedValue == "6")
        {
            RegularExpressionValidator Validator6 = (RegularExpressionValidator)FormView1.FindControl("Validator6");
            Validator6.Enabled = true;
            Validator6.Visible = true;
        }
        else if (dropDownlist_type.SelectedValue == "7")
        {
            RequiredFieldValidator Validator0 = (RequiredFieldValidator)FormView1.FindControl("Validator0");
            Validator0.Enabled = true;
            Validator0.Visible = true;
        }
        else if (dropDownlist_type.SelectedValue == "8")
        {
            RequiredFieldValidator Validator0 = (RequiredFieldValidator)FormView1.FindControl("Validator0");
            Validator0.Enabled = true;
            Validator0.Visible = true;
        }

    }
}
